package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.queue")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;


    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            System.out.println("收到过期的订单消息,准备关闭订单"+orderEntity.toString());
            orderService.closeOrder(orderEntity);
            //TODO 手动调用支付方收单

            //签收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //拒绝消息后重新发回队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
